package com.jfinal.aceadmin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

	public static long getMistiming(Date date) {
		Calendar cal = Calendar.getInstance();
		Date now = cal.getTime();
		long mistiming = now.getTime() - date.getTime();
		return mistiming;
	}

	public static Date parseDate(String dateString) {
		Date result = parseDate(dateString, "dd/MM/yyyy");
		return result;
	}

	public static Date parseDate(String dateString, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date result = new Date();
		try {
			result = sdf.parse(dateString);
		} catch (ParseException e) {

		}
		return result;
	}

	public static int getYearsBeforeNow(Date birthday) {
		Calendar cal = Calendar.getInstance();
		Calendar birthCal = Calendar.getInstance();
		birthCal.setTime(birthday);
		int age = cal.get(Calendar.YEAR) - birthCal.get(Calendar.YEAR);
		if(age == 0){
			age = 1;
		}
		return age;
	}

	public static String getAmPmDate(Date date) {
		SimpleDateFormat formatTime = new SimpleDateFormat("hh:mm aa",
				Locale.ENGLISH);
		return formatTime.format(date);
	}

}
